(function ($) {
	$.preMaterial = function () {
		return {
			coursewareData:[],
			init: function(){
				this.listener();
				this.preMaterialList();
			},
			/**
			 * 添加各按钮所需要的监听事件
			 */
			listener: function(){
				$(".dataCenterFile").on("click",function(){
					//iframe窗
					parent.layer.open({
						type: 2,
						title: "请选择要导入资料",
						area: ['700px', parent.window.innerHeight- 100 + "px"],
						content: "pageFrame/dataCenterList.html"
						, btn: ['确定']
						, yes: function (index, layero) {
							var inputs = parent.layer.getChildFrame(".dataList li input:checked", index);
							console.log(inputs);
							$.each(inputs,function(i,data){
								var html='';
								html+='<li>'
								+ '<a href='+$(data).parents("li").data("fileurl")+'>'+$(data).parents("li").data("filename")+'</a>'
								+ '<a class="button delFile delDom">删除</a>'
								+ '<a class="button uploadFile" href='+$(data).parents("li").data("fileurl")+'>下载</a>'
								+ '</li>';
								$(".hasMaterial").append(html);
							});
							parent.layer.close(index);
						}
					});
				});
				//点击上传 触发事件
				$(".upload a.button:first-child").on("click",function(){
					$("#uploadFile").click();
				});
				$("#uploadFile").on("change", function () {
					$(".uploadText").html("正在上传......");
					var list = document.getElementById("uploadFile").files;
					for (var index = 0; index < list.length; index++) {
						if (list[index]) {
							uploadFile(SystemConfig.fileUploadUrl+ SystemConfig.appKey + "/", list[index], function (result) {
								var html = "";
								var result = JSON.parse(result);
								if (result.status == 1000) {
									var docs = result.uploadFileDTO.fileId.split("/");
									var docName=docs[docs.length-1];
									html+='<li>'
										+ '<a href='+result.uploadFileDTO.fileId+'>'+docName+'</a>'
										+ '<a class="button delFile delDom">删除</a>'
										+ '<a class="button uploadFile" href='+result.uploadFileDTO.fileId+'>下载</a>'
										+ '</li>';
									$(".hasMaterial").append(html);
									$(".uploadText").html("ppt、word 模式");
								}
							});
						}
					}
					//置空file
					$(this).val("");
				});
			//	删除备课资料
				$(".hasMaterial").on("click",".delFile",function(){
					//判断是否只是删除dom 不删除数据库中数据
					$(this).parent().remove();
					if(!$(this).hasClass("delDom")){
						var docDTO = {
							"entityId": $(this).data("id")
						};
						delObject(Doman.url + Doman.courseDoc, true, JSON.stringify(docDTO), function (result) {
							parent.$.courseList.resourceList(true);
						});
					}
				});
			},


			preMaterialList: function(){
				if(parent.length=="1"){
                    $.preMaterial.coursewareData=parent.$.courseList.courseWareGroupDTOList[getParam("index")].coursewareData;
				}else{
                    $.preMaterial.coursewareData=parent[0].$.courseList.courseWareGroupDTOList[getParam("index")].coursewareData;
				}
				if($.preMaterial.coursewareData!=[]){
					var html="";
					$.each($.preMaterial.coursewareData,function(i,data){
						var docs = data.attachment.split("/");
						var docName=docs[docs.length-1];
						html+='<li>'
							+ '<a href='+data.attachment+'>'+docName+'</a>'
							+ '<a class="button delFile" data-id='+data.id+'>删除</a>'
							+ '<a class="button" href='+data.attachment+'>下载</a>'
							+ '</li>';
					});
					$(".hasMaterial").html(html);
				}
			}
		}
	}();
    $.preMaterial.init();
})(jQuery);